var express = require('express');
var router = express.Router();
const transactions = require('@liskhq/lisk-transactions');
const {getAddressAndPublicKeyFromPassphrase} = require('@liskhq/lisk-cryptography');

/* GET home page. */
router.post('/address', function(req, res) {
  const params = req.body;
  const address = getAddressAndPublicKeyFromPassphrase(params.privateKey);
  res.set('Content-Type', 'application/json');
  res.status(200).send(JSON.stringify(address));
});

router.post('/sign', function (req, res) {
    const body = req.body;
    const tx = transactions.transfer({
        amount: body.amount,
        recipientId: body.recipientId,
        passphrase: body.passphrase,
        networkIdentifier: body.networkIdentifier
    });
    res.set('Content-Type', 'application/json');
    res.status(200).send(JSON.stringify(tx));
});

router.get('/validateAddress/:address', function (req, res) {
    const params = req.params;
    res.set('Content-Type', 'application/json');
    try {
        const validate = transactions.utils.validateAddress(params.address);
        res.status(200).send('{\"result\":' + validate + '}');
    } catch (e) {
        res.status(200).send('{\"result\":false}');
    }
})


module.exports = router;
